<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'getlist';
$return=array();
$return['ischecklogin']=model('jy_weishop/member')->checklogin('','','','',true);
if(in_array($_SESSION['os'],array('baiduapp','aliapp','ttapp'))){
	exi('当前环境下不支持小程序直播!','error','jy_liveshop/index/');
}
if($_RQ['op']=='getlist'){
	$sale_live=$this->plugin_setting('basic');
	$live_id=intval($_RQ['live_id']);
	if($live_id){
		$this->session('live_id',$live_id);
	}
	$live_id=$this->session('live_id');
	if(!$live_id && $_SESSION['wxapp_id']){
		$live_id=pdo_getcolumn('core_account_wxapp_live',array('wxapp_id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']),'id');
		$this->session('live_id',$live_id);
	}
	if(!$live_id){
		if($sale_live['live_id']){
			$live_id=$sale_live['live_id'];
		}else{
			$live_id=pdo_getcolumn('core_account_wxapp_live',array('uniacid'=>$_SESSION['uniacid']),'id');
		}
		$this->session('live_id',$live_id);
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'live_id'=>$live_id,'mid'=>$_SESSION['uid']);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if($_RQ['roomsid']){
		$params['roomsid']=$_RQ['roomsid'];
		$return['params']['roomsid']=$_RQ['roomsid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$list=pdo_getslice('core_account_wxapp_live_rooms',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['image']=$item['cover_img']?tomedia($item['cover_img']):'';
			if($item['start_time']>time()){
				$item['liveing']=0;
				$item['liveingtitle']='还未开始';
			}elseif($item['end_time']<time()){
				$item['liveing']=2;
				$item['liveingtitle']='已经结束';
			}else{
				$item['liveing']=1;
				$item['liveingtitle']='直播中';
			}
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['start_time']=date('Y/m/d H:i:s',$item['start_time']);
			$item['grouptitle']=$fenlei[$item['group_id']]['title'];
			$item['member']=model('core/member')->detail($item['mid'],array('credit1','credit2','mobile','avatar','nickname'));
			$item['goods']=json_decode($item['goods'],true);
			$goods_ids=array();
			foreach($item['goods'] as $gv){
				$goods_ids[$gv['appgoodsid']]=$gv['appgoodsid'];
			}
			$item['goods_ids']=$goods_ids;
		}
	}
	$return['list']=$list?$list:array();
	$return['params']['page'] =$pindex;
	$return['post']=array('goods_ids'=>'');
	$return['member']=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'));
	$return['member']['avatar']=$return['member']['avatar']?tomedia($return['member']['avatar']):'';
	//$return['mytotalfee']=pdo_getsum('core_account_wxapp_live_rooms',array('mid'=>$_SESSION['uid']),'totalfee');
	$roomids=pdo_getall('core_account_wxapp_live_rooms',array('mid'=>$_SESSION['uid'],'live_id'=>$live_id,'uniacid'=>$_SESSION['uniacid']),'id','id');
	if($roomids){
		$roomids=array_keys($roomids);
		$total_count=pdo_count('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'status <'=>4,'roomid'=>$roomids),'id');
		$mytotalfee=pdo_getsum('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'status <'=>4,'roomid'=>$roomids),'total_price');
		$mytotalfee=price($mytotalfee*$sale_live['tichengrate']/100);
	}else{
		$total_count=0;
	}
	$return['mytotalfee']=$mytotalfee?:'0.00';
	$return['redmodal']=array(
		'title'=>'主播收入明细',
		'myallfee'=>$return['mytotalfee'],
		'total_count'=>$total_count
	);
	set_html('title','我的直播间');
	exi($return);
}
if($_RQ['op']=='setgoodstorooms'){
	$id=intval($_RQ['id']);
	$live_id=$this->session('live_id');
	if($_RQ['postdata']){
		$post=get_postdata();
		$goods_ids=array();
		foreach($post['goods_ids'] as $appgoodsid){
		    if($appgoodsid>0){
		        $goods_ids[]=$appgoodsid;
		    }
		}
		$roomid=$post['roomid'];
		if(!$roomid){
			$roomid=pdo_getsum('core_account_wxapp_live_rooms',array('id'=>$post['id']),'roomid');
		}
		$wxapp_id=pdo_getcolumn('core_account_wxapp_live',array('id'=>$live_id),'wxapp_id');
		$res=model('core/wxapp_api',$wxapp_id)->add_live_goodstorooms($goods_ids,$roomid);
		$goods=array();
		foreach($goods_ids as $appgoodsid){
			$goods[]=pdo_get('core_account_wxapp_live_goods',array('uniacid'=>$_SESSION['uniacid'],'appgoodsid'=>$appgoodsid),array('id','title','appgoodsid','image','price','ishuodong'));
		}
		$data=array(
			'goods'=>ijson_encode($goods),
		);
		pdo_update('core_account_wxapp_live_rooms',$data,array('id'=>$post['id']));
		exi('导入成功');
	}
}